<?php
/**
*
* ClassName: Queue_model
*
* 队列模型...
*
* @author hejigui <hejigui@soften.cn>
*
*/

class Queue_model extends CI_Model{
    var	$queue_name;	//队列名,用于rabbitmq
    var	$routing_key;	//队列的key

	/**
	* 实例化不同的队列对象
	*
	* @access public
	* @return string $mixed
	*/
	public function __construct() {
		parent::__construct();

		$this->load->config('rabbitmq');
        $config = $this->config->item('rabbitmq');
        $this->load->library('QueueRabbitMq',$config);
	}
	
	/**
	 * 入队列时的参数编码,最终返回约定过的编码格式(暂定json)
	 *
	 * @access public
	 * @param mixed $messages 消息内容.. [Must]
	 * @return string $mixed
	 */
	public function encode($messages) {
		if(empty($messages)){
			log_message('error',"Queue_model encode param is empty !!!");
			return false;
		}
	
		return json_encode($messages);
	}
	
	/**
	 * 出队列时的参数解码,与encode对应
	 * 通常返回值为数组..取决于 消息的格式
	 *
	 * @access public
	 * @param mixed $messages 消息内容.. [Must]
	 * @return array $mixed
	 */
	public function decode($messages) {
		if(empty($messages)){
			log_message('error',"Queue_model decode param is empty !!!");
			return false;
		}
	
		return json_decode($messages,true);
	}
	
	/**
	 * 获取exchange信息
	 *
	 * @access public
	 * @return string $mixed
	 */
	public function get_exchange() {
		return $this->config->item('exchange','rabbitmq');
	}
     
     public function add_weibo_task($taskinfo) {
     	if(empty($taskinfo) ){
     		log_message('error',"Queue_model add_shijue_task param is empty!!");
     		return false;
     	}
     	 
     	$exchange_name = $this->get_exchange();
     	$queue_name = 'weibo_task';
     	$routing_key = 'weibo_task_key';
     	 
     	$messages = $this->encode($taskinfo);
     	 
     	return $this->queuerabbitmq->add_task($messages, $exchange_name,$queue_name, $routing_key);
     }
      
      
     /**
      * 获取  需抓取关键词 任务信息
      *
      * @access public
      * @return void $mixed
      */
     public function get_weibo_task() {
     	$exchange_name = $this->get_exchange();
     	$queue_name = 'weibo_task';
     	$routing_key = 'weibo_task_key';
     	 
     	$messages = $this->queuerabbitmq->get_task($exchange_name,$queue_name, $routing_key);
     	return $messages;
     }


      public function add_weixin_task($taskinfo) {
        if(empty($taskinfo) ){
            log_message('error',"Queue_model add_shijue_weixin param is empty!!");
            return false;
        }
         
        $exchange_name = $this->get_exchange();
        $queue_name = 'wechat_task';
        $routing_key = 'wechat_task_key';
         
        $messages = $this->encode($taskinfo);
         
        return $this->queuerabbitmq->add_task($messages, $exchange_name,$queue_name, $routing_key);
     }
      
      
     /**
      * 获取  需抓取关键词 任务信息
      *
      * @access public
      * @return void $mixed
      */
     public function get_weixin_task() {
        $exchange_name = $this->get_exchange();
        $queue_name = 'wechat_task';
        $routing_key = 'wechat_task_key';
         
        $messages = $this->queuerabbitmq->get_task($exchange_name,$queue_name, $routing_key);
        return $messages;
     }

     public function add_autospider($taskinfo) {
        if(empty($taskinfo) ){
            log_message('error',"Queue_model add_autospider param is empty!!");
            return false;
        }
         
        $exchange_name = $this->get_exchange();
        $queue_name = 'autospider';
        $routing_key = 'autospider_key';
         
        $messages = $this->encode($taskinfo);
         
        return $this->queuerabbitmq->add_task($messages, $exchange_name,$queue_name, $routing_key);
     }


    /**
     * 初始化队列名和队列KEY
     *
     * @return void
     * @author Me
     */
    function init($queue_name)
    {
        if(empty($queue_name))
            return false;

        $this->queue_name = $queue_name;
        $this->routing_key = $queue_name.'_key';
    }

    /**
     * 添加 需要匹配的关键词
     * @param unknown_type $taskinfo
     */
    public function add_task($taskinfo) {
        if(empty($taskinfo) ){
            log_message('error',"Queue_model add_autospider param is empty!!");
            return false;
        }

        $exchange_name = $this->get_exchange();
        $queue_name = 'autospider_img_url';
        $routing_key = 'autospider_img_url_key';

        $messages = $this->encode($taskinfo);

        return $this->queuerabbitmq->add_task($messages, $exchange_name,$queue_name, $routing_key);
    }


    /**
     * 从队列中获取数据
     */
    public function get_task()
    {
        $exchange_name = $this->get_exchange();

        $queue_name = 'autospider_img_url';
        $routing_key = 'autospider_img_url_key';

        $messages = $this->queuerabbitmq->get_task($exchange_name,$queue_name, $routing_key);
        return $this->decode($messages) ;
    }


    public function add_cid_task($taskinfo) {
        if(empty($taskinfo) ){
            log_message('error',"Queue_model add_autospider param is empty!!");
            return false;
        }

        $exchange_name = $this->get_exchange();
        $queue_name = 'autospider_img_cid';
        $routing_key = 'autospider_img_cid_key';

        $messages = $this->encode($taskinfo);

        return $this->queuerabbitmq->add_task($messages, $exchange_name,$queue_name, $routing_key);
    }

    public function get_cid_task()
    {
        $exchange_name = $this->get_exchange();

        $queue_name = 'autospider_img_cid';
        $routing_key = 'autospider_img_cid_key';

        $messages = $this->queuerabbitmq->get_task($exchange_name,$queue_name, $routing_key);
        return $this->decode($messages) ;
    }

}

?>
